library(readxl)
imp <- read_excel("data/raw/importance_pledge_lit.xlsx")

library(ggplot2)
imp$percent_all <- as.numeric(imp$percent_all)
imp$percent_important <- as.numeric(imp$percent_important)
imp <- imp[imp$govparty==1, ]

mod <- lm(data = imp, percent_important~percent_all)
m <- summary(mod)
cor(imp$percent_all, imp$percent_important, use = "pairwise.complete.obs")


sum(!is.na(imp$percent_important))
library(mellonMisc)
imp.lit <- ggplot(data = imp, aes(x = percent_all, y = percent_important, colour = Authors, shape = Authors)) + 
  geom_point(size = 3) + theme_bes() + geom_abline(intercept = mod$coefficients["(Intercept)"], 
                                                      slope = mod$coefficients["percent_all"]) + 
  xlab("% promises fulfilled") + ylab("% major promises fulfilled") 



# Figure 1 Estimates of government party pledge fulfilment (partially/fully) for
# all promises (x-axis) and major promises (y-axis) from Costello et al. (2016), 
# Krukones (1980, 1984, 1985) and Royed (1996) (r2=0.07).
saveForPub(imp.lit, file = "figures/imp.lit", height  = 4)



# The mean absolute gap between the major-promise completion rate and the 
# overall completion rate is 28 percentage points, 
mean(abs(imp$percent_important - imp$percent_all), na.rm = T)

# with major promises completed at a 9 percentage points lower rate on average 
mean(imp$percent_important, na.rm = T) - mean(imp$percent_all, na.rm = T)
# across 18 elections covered by Krukones, Costello, O’Neill and Thomson, and 
# Royed. 
sum(!is.na(imp$percent_important))


# Perhaps more importantly, the overall promise-completion rate explains just 
# 7% of the variance in major-promise completion, meaning that unweighted 
# success tells us little about success in major promise completion. 
m$r.squared


# finding that major-promise completion barely correlates with overall completion (r2=0.07) 
m$r.squared
# and that the average absolute gap between major-promise completion and overall 
# promise completion is 28 percentage points.
mean(abs(imp$percent_important - imp$percent_all), na.rm = T)